import Cookies from "js-cookie";
import axios from "@/utils/requestdp";

import commonmixins from "@/mixins/bigScreen/commonmixins";

let mixin = {
  mixins: [commonmixins],
  props: {},
  mounted() {},
  data() {
    return {

    };
  },

  watch: {
  },

  methods: {
    switchnengxiaotype(n) {
      let _this = this;
      //判断geojson是否加载完毕

      //for两层 遍历多个数据源
      this.ds.map(item => {
        var entities = {};
        for (let index = 0; index < this._viewer.dataSources.length; index++) {
          if (item == this._viewer.dataSources.get(index)._name) {
            entities = this._viewer.dataSources.get(index).entities.values;
          }
        }

        for (var o = 0; o < entities.length; o++) {
          var entity = entities[o];
          entity.polygon.extrudedHeight = 0;
          entity.label = {}
          entity.idn = entity.id.split(".")[1];
          if (entity.polygon != undefined) {
            let rescolor = this.getefficcolor(
              n,
              entity.idn,
              this.$store.state.daping.efficiency.type,
              this.$store.state.daping.efficiency.colorvalmap,
              this.$store.state.daping.efficiency.colors
            );

            entity.polygon.material = Cesium.Color.fromCssColorString(rescolor);
            entity.polygon.outline = false;

            let scal = 1;

            switch (_this.$store.state.daping.efficiency.type) {
              case "m_001t":
              case "m_002t":
              case "m_004t_1":
              case "m_019t_1":
                scal = 3;
                break;
              case "m_003p":
                scal = 0.5;
                break;
              case "m_001p":
                scal = 100;
                break;
              case "m_003qc":
                scal = 1;
                break;

              default:
                break;
            }

            //注意这里添加
            var polyPositions = entity.polygon.hierarchy.getValue(
              Cesium.JulianDate.now()
            ).positions;
            var polyCenter = Cesium.BoundingSphere.fromPoints(polyPositions)
              .center;
            polyCenter = Cesium.Ellipsoid.WGS84.scaleToGeodeticSurface(
              polyCenter
            );
            entity.position = polyCenter;

            if (
              n[Number(entity.idn)] &&
              n[Number(entity.idn)][0][_this.$store.state.daping.efficiency.type]
            ) {
              let resheight =
                Number(Number(
                  n[Number(entity.idn)][0][
                    _this.$store.state.daping.efficiency.type
                  ]
                ).toFixed(2)) * scal;

              if (resheight < 0) {
                resheight = 0;
              }

              if (resheight > 100) {
                resheight = 100;
              }

              entity.polygon.extrudedHeight = resheight;

              var ellipsoid = _this._viewer.scene.globe.ellipsoid;
              let reslabelposition = Cesium.Cartographic.fromCartesian(
                entity.position._value
              );
              reslabelposition.height = Number(Number(resheight).toFixed(0));
              var cartesian3 = ellipsoid.cartographicToCartesian(reslabelposition);
              entity.position = cartesian3;

              entity.label = {
                text:
                  n[Number(entity.idn)][0]["name"] +
                  ": " +
                  Number(
                    n[Number(entity.idn)][0][
                      _this.$store.state.daping.efficiency.type
                    ]
                  ).toFixed(2) +
                  "",

                fillColor: Cesium.Color.fromCssColorString("#A0c8DB"),
                font: "normal " + this.getscalepxcnum(8) + "px MicroSoft YaHei",
                showBackground: true,
                backgroundColor: Cesium.Color.fromCssColorString("#0D2654"),
                scale: 1.0,
                backgroundPadding: new Cesium.Cartesian2(7, 5),
                horizontalOrigin: Cesium.HorizontalOrigin.LEFT_CLICK,
                verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
                // distanceDisplayCondition: new Cesium.DistanceDisplayCondition(
                //   10.0,
                //   20000.0
                // ),
                disableDepthTestDistance: 0,
                scaleByDistance: new Cesium.NearFarScalar(0, 1.5, 52820, 0.01)
                // translucencyByDistance: 100,
                // heightReference: 100,
              };
            }
          }
        }
      });
    },
    
    nengxiao() {
      let _this = this;
      var viewer = this._earth.czm.viewer;
      //判断是否有缓存有的话就加载没有的话就发请求获取

      this.getModelval("nengxiaoModal", resmc => {
        this.loadnengxiaojson(JSON.parse(resmc));
      });
      
    },

    loadnengxiaojson(data) {
      let _this = this;
      var viewer = this._earth.czm.viewer;
      //加载数据源
      var promise = Cesium.GeoJsonDataSource.load(data);
      promise.then(function(dataSource) {
        viewer.dataSources.add(dataSource);
        _this.ds.push(dataSource._name);
        if (_this.$root.efficiency_data != {}) {
          _this.switchnengxiaotype(_this.$root.efficiency_data["efficiencydata"]);
        }
      });
    },

    getefficcolor(thisRetMap, spid, curType, rangeArr, colors) {

      var color = colors[colors.length - 1];
      // if (thisRetMap[Number(spid)] != null) {
      //   if (thisRetMap[Number(spid)][curType] > rangeArr[0]) {
      //     color = colors[0];
      //   } else if (thisRetMap[Number(spid)][curType] > rangeArr[1]) {
      //     color = colors[1];
      //   } else if (thisRetMap[Number(spid)][curType] > rangeArr[2]) {
      //     color = colors[2];
      //   } else if (thisRetMap[Number(spid)][curType] > rangeArr[3]) {
      //     color = colors[3];
      //   } else if (thisRetMap[Number(spid)][curType] > rangeArr[4]) {
      //     color = colors[4];
      //   } else if (thisRetMap[Number(spid)][curType] > rangeArr[5]) {
      //     color = colors[5];
      //   } else if (thisRetMap[Number(spid)][curType] > rangeArr[6]) {
      //     color = colors[6];
      //   } else if (thisRetMap[Number(spid)][curType] > rangeArr[7]) {
      //     color = colors[7];
      //   } else if (thisRetMap[Number(spid)][curType] > rangeArr[8]) {
      //     color = colors[8];
      //   }
      // }

      // let ddd = '90+@@80-90@@70-80@@60-70@@50-60@@40-50@@30-40@@20-30@@10-20@@10-'
      let distddd = rangeArr
      if (thisRetMap[Number(spid)] != null) {
        let vvv = thisRetMap[Number(spid)][0][curType]
        distddd.map((item, index) => {
          if (item.indexOf("≥") != -1) {
            let numcompare = item.split("≥")[1] * 1
            if (vvv > numcompare) {
              color = colors[0];
            }
          }else if (item.indexOf("<") != -1){
            let numcompare = item.split("<")[1] * 1
            if (vvv < numcompare) {
              color = colors[colors.length - 1]
            }
          }else{
            let numcompareleft = item.split("~")[0] * 1 
            let numcompareright = item.split("~")[1] * 1 
            if (vvv > numcompareleft && vvv <= numcompareright) {
              color = colors[index]
            }
          }
        })
      }

      return color;
    }
  }
};

export default mixin;
